package cn.ipanel.bigdata.dw.realtime

import cn.ipanel.bigdata.boot.source.genre.Phoenix
import cn.ipanel.bigdata.dw.DetailColumns
import cn.ipanel.bigdata.dw.dim.phoenix.saltBuckets
import cn.ipanel.bigdata.dw.dws.HBASE_NAMESPACE
import cn.ipanel.bigdata.utils.Dictionary
import cn.ipanel.bigdata.utils.Dictionary.DeviceStat._
import org.apache.spark.sql.{Dataset, Row}

/**
 * @author lzz
 * @environment IntelliJ IDEA 2020.3.1
 * @projectName bigdata_panyu   番禺大数据，区域数据
 * @date 2023/11/14 16:15
 * @description:
 */
protected[realtime] object DeviceOnlineStatus extends Phoenix(HBASE_NAMESPACE, "t_device_online_status") with DetailColumns {

  final val F_STATUS                    : String = Dictionary.F_STATUS
  final val F_STATUS_ONLINE             : Integer = ONLINE
  final val F_STATUS_OFFLINE            : Integer = OFFLINE

  override def getTBColumns: Seq[String] = Seq(
    F_DEVICE_ID, F_STATUS, F_UPDATE_TIME
  )

  override def buildTable: String = {
    s"""
       |CREATE TABLE IF NOT EXISTS $getDBName.$getTBName(
       |$F_DEVICE_ID VARCHAR NOT NULL
       |, $F_STATUS TINYINT
       |, $F_UPDATE_TIME BIGINT
       |CONSTRAINT PK PRIMARY KEY($F_DEVICE_ID)
       |) SALT_BUCKETS = $saltBuckets;
       |""".stripMargin
  }

  override def emptyTable: Dataset[_] = {
    import IMPLICITS._
    spark.createDataset(spark.sparkContext.emptyRDD[DeviceOnlineStatusTable])
  }

  case class DeviceOnlineStatusTable(var f_period_time: String
                                     , var f_status: Byte
                                     , var f_update_time: Long
                       )

  object DeviceOnlineStatusTable {
    def apply(row: Row): DeviceOnlineStatusTable = {
      DeviceOnlineStatusTable(row.getAs[String](F_DEVICE_ID)
        , row.getAs[Byte](F_STATUS)
        , row.getAs[Long](F_UPDATE_TIME)
      )
    }
  }
}